<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="姓名" prop="pc">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="pc">
        <el-input
          v-model="queryParams.tel"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属位置" prop="source" v-if="flag != 0">
        <el-select
          v-model="queryParams.source"
          placeholder="所属位置"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in sourceList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="星级" prop="level">
        <el-select v-model="queryParams.level" placeholder="请选择星级" clearable style="width: 160px">
          <el-option v-for="dict in dict.type.level_type" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="错误类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="登录状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.export_matter"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-update"
          @click="updateRemark"
          :disabled="this.ids.length == 0"
          v-hasPermi="['log:exportLog:updateRemark']"
        >更新备注
        </el-button>
      </el-col>


      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-update"
          @click="updateCustomerName"
          :disabled="this.ids.length == 0"
          v-hasPermi="['log:exportLog:updateCustomerName']"
        >更新姓名
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-update"
          @click="updateCustomerLevel"
          :disabled="this.ids.length == 0"
          v-hasPermi="['log:exportLog:updateCustomerLevel']"
        >更新星级
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-update"
          @click="updateCustomerStatus"
          :disabled="this.ids.length == 0"
          v-hasPermi="['log:exportLog:updateCustomerStatus']"
        >更新状态
        </el-button>
      </el-col>


      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          @click="oneKeyIssue"
          :disabled="this.ids.length == 0"
          v-hasPermi="['total:customer:oneKeyIssue']"
        >一键下发
        </el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="exportList" @selection-change="handleSelectionChange" height="600px">
      <el-table-column type="selection" :selectable="isOk" align="center"/>
      <el-table-column label="ID" align="center" prop="id" v-if="false"/>
      <el-table-column label="姓名" align="center" prop="name" fixed="left"/>
      <el-table-column label="手机号码" align="center" width="150" prop="tel" fixed="left"/>
      <el-table-column label="星级" align="center" prop="level">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.level_type" :value="scope.row.level"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="bz" width="400" show-overflow-tooltip/>

      <el-table-column label="已有姓名" align="center" width="150" prop="customerName" v-if="flag != 0" show-overflow-tooltip/>
      <el-table-column label="已有备注" align="center" width="150" prop="customerBz" v-if="flag != 0" show-overflow-tooltip/>
<!--      <el-table-column label="用户性别" align="center" prop="sex"/>-->
      <el-table-column label="位置" align="center" prop="source" v-if="flag != 0">
        <template slot-scope="scope">
          <span> {{ scope.row.source == 1 ? '已下发' : '总库' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="业务员" align="center" prop="distributeUser" v-if="flag != 0"/>
      <el-table-column label="部门" align="center" prop="deptName" v-if="flag != 0"/>
      <el-table-column label="错误信息" align="center" prop="errMsg" v-if="flag != 0" fixed="right">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.export_matter" :value="scope.row.flag"/>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <el-dialog title="更新备注" :visible.sync="log.open" width="90%" append-to-body>
      <!-- <el-form> -->
      <el-row>
        <el-input
          type="textarea"
          :rows="4"
          v-model="remark"
          placeholder="请输入备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-row>
      <!-- </el-form> -->
      <div slot="footer" class="dialog-footer">
        <el-button :loading="isLoading" type="primary" @click="submitReark">确 定</el-button>
        <el-button @click="log.open = false">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog title="更新客户姓名" :visible.sync="customerName.open" width="90%" append-to-body>
      <!-- <el-form> -->
      <el-row>
        <el-input
          type="textarea"
          :rows="4"
          v-model="customerNameInfo"
          placeholder="请输入客户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-row>
      <!-- </el-form> -->
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitCustomerName">确 定</el-button>
        <el-button @click="customerName.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="更新客户星级" :visible.sync="customerLevelInfo.open" width="100" append-to-body>
      <!-- <el-form> -->
      <el-row :style="{'--form-item-width': '100%'}">
        <el-select v-model="customerLevel" placeholder="请选择星级">
          <el-option
            v-for="dict in dict.type.level_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-row>
      <!-- </el-form> -->
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitCustomerLevel">确 定</el-button>
        <el-button @click="customerLevelInfo.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="更新客户状态" :visible.sync="customerStatusInfo.open" width="100" append-to-body>
      <!-- <el-form> -->
      <el-row :style="{'--form-item-width': '100%'}">
        <el-select v-model="customerStatus" placeholder="请选择星级">
          <el-option
            v-for="dict in dict.type.customer_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-row>
      <!-- </el-form> -->
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitCustomerStatus">确 定</el-button>
        <el-button @click="customerStatusInfo.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 分配客户信息 -->
    <bath-dis-customer ref="bathDisCustomer"></bath-dis-customer>
  </div>
</template>
<script>
import {listLog, submitReark, submitCustomerName, submitCustomerLevel, submitCustomerStatus} from "@/api/exportLog";
import BathDisCustomer from '@/views/components/BathDisCustomer.vue'
import {updateCustomer} from "@/api/customer/customer";

export default {
  dicts: ['export_matter', 'level_type', 'customer_status'],
  name: "ExportLog",
  components: {
    BathDisCustomer
  },
  props: ['pc', 'flag'],
  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 导入记录表格数据
      exportList: [],
      userList: [],
      deptList: [],
      sourceList: [
        {
          value: "1",
          label: "已下发"
        },
        {
          value: "0",
          label: "总库"
        },
      ],
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        pc: '',
        tel: undefined,
        name: '',
        type: '',
        lb: '',
        level: '',
        source: '',
      },
      remark: '',
      log: {
        open: false
      },
      isLoading: false,
      customerName: {
        open: false
      },
      customerNameInfo: '',

      customerLevelInfo: {
        open: false
      },
      customerLevel: '',

      customerStatusInfo: {
        open: false
      },
      customerStatus: '',
    }
  },
  created() {
    this.queryParams.name = ''
    this.queryParams.tel = ''
    this.queryParams.type = ''
    this.queryParams.level = ''
    this.getList();
  },
  methods: {
    /** 查询导入记录列表 */
    getList() {
      this.loading = true;
      this.queryParams.flag = this.flag
      this.queryParams.pc = this.pc
      console.log('aaa', this.queryParams)
      listLog(this.queryParams).then(response => {
        this.exportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    updateRemark() {
      this.remark = ''
      this.log.open = true
    },
    updateCustomerName() {
      this.customerNameInfo = '';
      this.customerName.open = true;
    },
    updateCustomerLevel() {
      this.customerLevel = '';
      this.customerLevelInfo.open = true;
    },
    updateCustomerStatus() {
      this.customerStatus = '';
      this.customerStatusInfo.open = true;
    },

    submitCustomerStatus() {
      if (this.customerStatus == '') {
        this.$modal.msgError("请选择一个状态");
        return
      }
      let data = {};
      data.ids = this.ids
      data.customerStatus = this.customerStatus
      data.batchLogo = this.pc
      submitCustomerStatus(data).then(res => {
        if (res && res.code == '200') {
          this.$modal.msgSuccess("修改成功");
          this.customerStatusInfo.open = false
          this.customerStatus = ''
          this.getList();
        }
      })
    },


    submitCustomerLevel() {
      if (this.customerLevel == '') {
        this.$modal.msgError("请选择一个星级");
        return
      }
      let data = {};
      data.ids = this.ids
      data.customerLevel = this.customerLevel
      data.batchLogo = this.pc
      submitCustomerLevel(data).then(res => {
        if (res && res.code == '200') {
          this.$modal.msgSuccess("修改成功");
          this.customerLevelInfo.open = false
          this.customerLevel = ''
          this.getList();
        }
      })
    },
    submitCustomerName() {
      if (this.customerNameInfo == '') {
        this.$modal.msgError("请输入客户姓名");
        return
      }
      let data = {}
      data.ids = this.ids
      data.customerName = this.customerNameInfo
      data.batchLogo = this.pc
      submitCustomerName(data).then(res => {
        if (res && res.code == '200') {
          this.$modal.msgSuccess("修改成功");
          this.customerName.open = false
          this.customerNameInfo = ''
          this.getList();
        }
      })
    },
    submitReark() {
      if (this.remark == '') {
        this.$modal.msgError("请输入客户备注");
        return
      }
      let data = {}
      data.ids = this.ids
      data.remark = this.remark
      data.batchLogo = this.pc
      submitReark(data).then(res => {
        if (res && res.code == '200') {
          this.$modal.msgSuccess("修改成功");
          this.log.open = false
          this.remark = ''
          this.getList();
        }
      })
    },
    isOk(row) {
      return row.flag != '1' || row.flag == '2'
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    oneKeyIssue() {
      if (this.ids.length == 0) {
        this.$modal.msgError("请选择用户！");
        return;
      }
      const ids = this.ids
      const customers = [];
      ids.forEach(it => {
        const obj = this.exportList.find(item => item.id == it);
        obj.pc = this.pc;
        customers.push(obj)
      })
      // 跳转分配
      this.$refs.bathDisCustomer.openDialog(customers, 'exportInfo');
    },

    /** 获取用户信息 */
    getUserInfoList() {
      const query = {
        type: "1"
      }
      getUserList(query).then(response => {
        this.userList = response.data;
      })
    },
    getDeptInfoList() {
      const query = {
        type: "1"
      }
      getDeptList(query).then(response => {
        this.deptList = response.data;
      })
    },

  }
}
</script>
<style>
.el-dialog__body {
   padding: 5px 20px;
}
</style>
